﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using HYSoft.IDAL;
using Maticsoft.DBUtility;
using System.Web;
namespace HYSoft.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:user
	/// </summary>
    public partial class user : Iuser
	{
		public user()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("index", "user"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int index)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from [user]");
			strSql.Append(" where [index]=@index");
			SqlParameter[] parameters = {
					new SqlParameter("@index", SqlDbType.Int,4)
			};
			parameters[0].Value = index;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}

        /// <summary>
        /// 是否存在该用户
        /// </summary>
        public bool Existsusername(Model.user umodel)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [user]");
            strSql.Append(" where loginname=@loginname");
            SqlParameter[] parameters = {
					new SqlParameter("@loginname", SqlDbType.VarChar,255)
			};
            parameters[0].Value = umodel.loginname;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public HYSoft.Model.user Login(Model.user model)
        {
            StringBuilder strSql=new StringBuilder();
			strSql.Append("select * from [user]");
			strSql.Append(" where loginname=@loginname");
            strSql.Append(" and passwd=@passwd and type=@type");
			SqlParameter[] parameters = {
					new SqlParameter("@loginname", SqlDbType.VarChar,255),
					new SqlParameter("@passwd", SqlDbType.VarChar,255),
					new SqlParameter("@type", SqlDbType.Int,4)
			};
			parameters[0].Value = model.loginname;
            parameters[1].Value = model.passwd;
            parameters[2].Value = model.type;
            if (DbHelperSQL.Exists(strSql.ToString(), parameters))
            {
                DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            return null;
        }
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(HYSoft.Model.user model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into [user](");
			strSql.Append("loginname,passwd,unit,addr,postcode,m_area_name,m_area_id,name,mobile,telephone,fax,email,type,intvalue1,intvalue2,intvalue3,charvalue1,charvalue2,charvalue3)");
			strSql.Append(" values (");
			strSql.Append("@loginname,@passwd,@unit,@addr,@postcode,@m_area_name,@m_area_id,@name,@mobile,@telephone,@fax,@email,@type,@intvalue1,@intvalue2,@intvalue3,@charvalue1,@charvalue2,@charvalue3)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@loginname", SqlDbType.VarChar,255),
					new SqlParameter("@passwd", SqlDbType.VarChar,255),
					new SqlParameter("@unit", SqlDbType.VarChar,255),
					new SqlParameter("@addr", SqlDbType.VarChar,255),
					new SqlParameter("@postcode", SqlDbType.VarChar,50),
					new SqlParameter("@m_area_name", SqlDbType.VarChar,50),
					new SqlParameter("@m_area_id", SqlDbType.VarChar,50),
					new SqlParameter("@name", SqlDbType.VarChar,50),
					new SqlParameter("@mobile", SqlDbType.VarChar,50),
					new SqlParameter("@telephone", SqlDbType.VarChar,50),
					new SqlParameter("@fax", SqlDbType.VarChar,50),
					new SqlParameter("@email", SqlDbType.VarChar,50),
					new SqlParameter("@type", SqlDbType.Int,4),
					new SqlParameter("@intvalue1", SqlDbType.Int,4),
					new SqlParameter("@intvalue2", SqlDbType.Int,4),
					new SqlParameter("@intvalue3", SqlDbType.Int,4),
					new SqlParameter("@charvalue1", SqlDbType.VarChar,255),
					new SqlParameter("@charvalue2", SqlDbType.VarChar,255),
					new SqlParameter("@charvalue3", SqlDbType.VarChar,255)};
			parameters[0].Value = model.loginname;
			parameters[1].Value = model.passwd;
			parameters[2].Value = model.unit;
			parameters[3].Value = model.addr;
			parameters[4].Value = model.postcode;
			parameters[5].Value = model.m_area_name;
			parameters[6].Value = model.m_area_id;
			parameters[7].Value = model.name;
			parameters[8].Value = model.mobile;
			parameters[9].Value = model.telephone;
			parameters[10].Value = model.fax;
			parameters[11].Value = model.email;
			parameters[12].Value = model.type;
			parameters[13].Value = model.intvalue1;
			parameters[14].Value = model.intvalue2;
			parameters[15].Value = model.intvalue3;
			parameters[16].Value = model.charvalue1;
			parameters[17].Value = model.charvalue2;
			parameters[18].Value = model.charvalue3;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(HYSoft.Model.user model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update [user] set ");
			strSql.Append("loginname=@loginname,");
			strSql.Append("passwd=@passwd,");
			strSql.Append("unit=@unit,");
			strSql.Append("addr=@addr,");
			strSql.Append("postcode=@postcode,");
			strSql.Append("m_area_name=@m_area_name,");
			strSql.Append("m_area_id=@m_area_id,");
			strSql.Append("name=@name,");
			strSql.Append("mobile=@mobile,");
			strSql.Append("telephone=@telephone,");
			strSql.Append("fax=@fax,");
			strSql.Append("email=@email,");
			strSql.Append("type=@type,");
			strSql.Append("intvalue1=@intvalue1,");
			strSql.Append("intvalue2=@intvalue2,");
			strSql.Append("intvalue3=@intvalue3,");
			strSql.Append("charvalue1=@charvalue1,");
			strSql.Append("charvalue2=@charvalue2,");
			strSql.Append("charvalue3=@charvalue3");
			strSql.Append(" where [index]=@index");
			SqlParameter[] parameters = {
					new SqlParameter("@loginname", SqlDbType.VarChar,255),
					new SqlParameter("@passwd", SqlDbType.VarChar,255),
					new SqlParameter("@unit", SqlDbType.VarChar,255),
					new SqlParameter("@addr", SqlDbType.VarChar,255),
					new SqlParameter("@postcode", SqlDbType.VarChar,50),
					new SqlParameter("@m_area_name", SqlDbType.VarChar,50),
					new SqlParameter("@m_area_id", SqlDbType.VarChar,50),
					new SqlParameter("@name", SqlDbType.VarChar,50),
					new SqlParameter("@mobile", SqlDbType.VarChar,50),
					new SqlParameter("@telephone", SqlDbType.VarChar,50),
					new SqlParameter("@fax", SqlDbType.VarChar,50),
					new SqlParameter("@email", SqlDbType.VarChar,50),
					new SqlParameter("@type", SqlDbType.Int,4),
					new SqlParameter("@intvalue1", SqlDbType.Int,4),
					new SqlParameter("@intvalue2", SqlDbType.Int,4),
					new SqlParameter("@intvalue3", SqlDbType.Int,4),
					new SqlParameter("@charvalue1", SqlDbType.VarChar,255),
					new SqlParameter("@charvalue2", SqlDbType.VarChar,255),
					new SqlParameter("@charvalue3", SqlDbType.VarChar,255),
					new SqlParameter("@index", SqlDbType.Int,4)};
			parameters[0].Value = model.loginname;
			parameters[1].Value = model.passwd;
			parameters[2].Value = model.unit;
			parameters[3].Value = model.addr;
			parameters[4].Value = model.postcode;
			parameters[5].Value = model.m_area_name;
			parameters[6].Value = model.m_area_id;
			parameters[7].Value = model.name;
			parameters[8].Value = model.mobile;
			parameters[9].Value = model.telephone;
			parameters[10].Value = model.fax;
			parameters[11].Value = model.email;
			parameters[12].Value = model.type;
			parameters[13].Value = model.intvalue1;
			parameters[14].Value = model.intvalue2;
			parameters[15].Value = model.intvalue3;
			parameters[16].Value = model.charvalue1;
			parameters[17].Value = model.charvalue2;
			parameters[18].Value = model.charvalue3;
			parameters[19].Value = model.index;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int index)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from [user] ");
			strSql.Append(" where [index]=@index");
			SqlParameter[] parameters = {
					new SqlParameter("@index", SqlDbType.Int,4)
			};
			parameters[0].Value = index;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string indexlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from [user] ");
			strSql.Append(" where [index] in ("+indexlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public HYSoft.Model.user GetModel(int index)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 [index],loginname,passwd,unit,addr,postcode,m_area_name,m_area_id,name,mobile,telephone,fax,email,type,intvalue1,intvalue2,intvalue3,charvalue1,charvalue2,charvalue3 from [user] ");
			strSql.Append(" where [index]=@index");
			SqlParameter[] parameters = {
					new SqlParameter("@index", SqlDbType.Int,4)
			};
			parameters[0].Value = index;

			HYSoft.Model.user model=new HYSoft.Model.user();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public HYSoft.Model.user DataRowToModel(DataRow row)
		{
			HYSoft.Model.user model=new HYSoft.Model.user();
			if (row != null)
			{
				if(row["index"]!=null && row["index"].ToString()!="")
				{
					model.index=int.Parse(row["index"].ToString());
				}
				if(row["loginname"]!=null)
				{
					model.loginname=row["loginname"].ToString();
				}
				if(row["passwd"]!=null)
				{
					model.passwd=row["passwd"].ToString();
				}
				if(row["unit"]!=null)
				{
					model.unit=row["unit"].ToString();
				}
				if(row["addr"]!=null)
				{
					model.addr=row["addr"].ToString();
				}
				if(row["postcode"]!=null)
				{
					model.postcode=row["postcode"].ToString();
				}
				if(row["m_area_name"]!=null)
				{
					model.m_area_name=row["m_area_name"].ToString();
				}
				if(row["m_area_id"]!=null)
				{
					model.m_area_id=row["m_area_id"].ToString();
				}
				if(row["name"]!=null)
				{
					model.name=row["name"].ToString();
				}
				if(row["mobile"]!=null)
				{
					model.mobile=row["mobile"].ToString();
				}
				if(row["telephone"]!=null)
				{
					model.telephone=row["telephone"].ToString();
				}
				if(row["fax"]!=null)
				{
					model.fax=row["fax"].ToString();
				}
				if(row["email"]!=null)
				{
					model.email=row["email"].ToString();
				}
				if(row["type"]!=null && row["type"].ToString()!="")
				{
					model.type=int.Parse(row["type"].ToString());
				}
				if(row["intvalue1"]!=null && row["intvalue1"].ToString()!="")
				{
					model.intvalue1=int.Parse(row["intvalue1"].ToString());
				}
				if(row["intvalue2"]!=null && row["intvalue2"].ToString()!="")
				{
					model.intvalue2=int.Parse(row["intvalue2"].ToString());
				}
				if(row["intvalue3"]!=null && row["intvalue3"].ToString()!="")
				{
					model.intvalue3=int.Parse(row["intvalue3"].ToString());
				}
				if(row["charvalue1"]!=null)
				{
					model.charvalue1=row["charvalue1"].ToString();
				}
				if(row["charvalue2"]!=null)
				{
					model.charvalue2=row["charvalue2"].ToString();
				}
				if(row["charvalue3"]!=null)
				{
					model.charvalue3=row["charvalue3"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select [index],loginname,passwd,unit,addr,postcode,m_area_name,m_area_id,name,mobile,telephone,fax,email,type,intvalue1,intvalue2,intvalue3,charvalue1,charvalue2,charvalue3 ");
			strSql.Append(" FROM [user] ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" [index],loginname,passwd,unit,addr,postcode,m_area_name,m_area_id,name,mobile,telephone,fax,email,type,intvalue1,intvalue2,intvalue3,charvalue1,charvalue2,charvalue3 ");
			strSql.Append(" FROM [user] ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM [user] ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.index desc");
			}
			strSql.Append(")AS Row, T.*  from [user] T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "user";
			parameters[1].Value = "index";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

